﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Vanfa.Queries.Sql.DbModel;

namespace Vanfa.Queries.Sql.DbQueries
{
    /// <summary>
    /// 用户管理数据库操作
    /// </summary>
    public class UserQry : TDbImpl<UserQry>
    {
        /// <summary>
        /// 插入用户基本信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int Insert(userMember user)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_User_Creation");
            VanfaDb.AddInParameter(cmd, "UserName", DbType.AnsiString, user.UserName);
            VanfaDb.AddInParameter(cmd, "Password", DbType.AnsiString, user.Password);
            VanfaDb.AddInParameter(cmd, "UserType", DbType.Int32, user.UserType);
            VanfaDb.AddInParameter(cmd, "GradeID", DbType.Int32, user.GradeID);
            VanfaDb.AddInParameter(cmd, "Mobile", DbType.AnsiString, user.Mobile);
            VanfaDb.AddInParameter(cmd, "Gender", DbType.Boolean, user.Gender);
            var ret = VanfaDb.ExecuteScalar(cmd);
            return Convert.ToInt32(ret);
        }

        /// <summary>
        /// 用户登录记录
        /// </summary>
        /// <param name="ulogin"></param>
        /// <returns></returns>
        public int Insert(userLoginHistory ulogin)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_User_Logining");
            VanfaDb.AddInParameter(cmd, "UserID", DbType.Int32, ulogin.UserID);
            return VanfaDb.ExecuteNonQuery(cmd);
        }

        /// <summary>
        /// 查找用户信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public userMember Select(string value, int type = 1)
        {
            string sp, param;
            if (type == 1)
            {
                sp = "up_User_SelectByName";
                param = "UserName";
            }
            else
            {
                sp = "up_User_SelectByMobile";
                param = "Mobile";
            }

            var cmd = VanfaDb.GetStoredProcCommand(sp);
            VanfaDb.AddInParameter(cmd, param, DbType.AnsiString, value);
            using (var read = VanfaDb.ExecuteReader(cmd))
            {
                if (!read.Read())
                    return null;
                return this.ConvertTo<userMember>(read);
            }
        }

        /// <summary>
        /// 根据用户ID获取用户信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public userMember Select(int userID)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_User_SelectByID");
            VanfaDb.AddInParameter(cmd, "UserID", DbType.AnsiString, userID);
            using (var read = VanfaDb.ExecuteReader(cmd))
            {
                if (!read.Read())
                    return null;
                return this.ConvertTo<userMember>(read);
            }
        }

        /// <summary>
        /// 插入验证码
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public int Insert(string mobile, string code)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_User_CreateCheckCode");
            VanfaDb.AddInParameter(cmd, "Mobile", DbType.AnsiString, mobile);
            VanfaDb.AddInParameter(cmd, "Code", DbType.AnsiString, code);
            return VanfaDb.ExecuteNonQuery(cmd);
        }

        public userCheckCode SelectCode(string mobile)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_User_GetCheckCodeByMobile");
            VanfaDb.AddInParameter(cmd, "Mobile", DbType.AnsiString, mobile);
            using (var read = VanfaDb.ExecuteReader(cmd))
            {
                if (!read.Read())
                    return null;
                return this.ConvertTo<userCheckCode>(read);
            }
        }

        /// <summary>
        /// 插入个人信息
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="mp"></param>
        /// <param name="uf"></param>
        /// <param name="uc"></param>
        /// <returns></returns>
        public int Insert(int userID, userMemberInfo_Private mp, userMemberFilter uf, userMemberInfo_Contact uc)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_User_FillPrivateInfo");
            VanfaDb.AddInParameter(cmd, "UserID", DbType.Int32, userID);
            VanfaDb.AddInParameter(cmd, "FullName", DbType.AnsiString, mp.FullName);
            VanfaDb.AddInParameter(cmd, "IDCard", DbType.AnsiString, mp.IDCard);
            VanfaDb.AddInParameter(cmd, "Icon", DbType.AnsiString, mp.IconPath);
            VanfaDb.AddInParameter(cmd, "Mobile1", DbType.AnsiString, uc.MobilePhone);
            VanfaDb.AddInParameter(cmd, "Mobile2", DbType.AnsiString, uc.iPhone);
            VanfaDb.AddInParameter(cmd, "qq", DbType.AnsiString, uc.QQ);
            VanfaDb.AddInParameter(cmd, "wx", DbType.AnsiString, uc.Wechat);
            VanfaDb.AddInParameter(cmd, "wb", DbType.AnsiString, uc.SinaWebo);
            VanfaDb.AddInParameter(cmd, "Email", DbType.AnsiString, uc.Email);
            VanfaDb.AddInParameter(cmd, "Birthday", DbType.DateTime, mp.Birthday);
            VanfaDb.AddInParameter(cmd, "Location", DbType.AnsiString, mp.HomeTown);
            VanfaDb.AddInParameter(cmd, "Height", DbType.Int32, uf.Height);
            VanfaDb.AddInParameter(cmd, "Weight", DbType.Int32, uf.Weight);
            VanfaDb.AddInParameter(cmd, "P1", DbType.Int32, uf.P1);
            VanfaDb.AddInParameter(cmd, "P2", DbType.Int32, uf.P2);
            VanfaDb.AddInParameter(cmd, "P3", DbType.Int32, uf.P3);
            VanfaDb.AddInParameter(cmd, "P4", DbType.Int32, uf.P4);
            VanfaDb.AddInParameter(cmd, "P5", DbType.Int32, uf.P5);
            VanfaDb.AddInParameter(cmd, "P6", DbType.Int32, uf.P6);
            VanfaDb.AddInParameter(cmd, "P7", DbType.AnsiString, uf.P7);
            VanfaDb.AddInParameter(cmd, "NickName", DbType.AnsiString, mp.NickName);
            return VanfaDb.ExecuteNonQuery(cmd);
        }

        public int Insert(int userID, userMemberInfo_Group mg, userMemberInfo_Contact uc)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_User_FillGroupInfo");
            VanfaDb.AddInParameter(cmd,"UserID", DbType.Int32,userID);
            VanfaDb.AddInParameter(cmd,"Agent", DbType.AnsiString,mg.Agent);
            VanfaDb.AddInParameter(cmd,"GroupName", DbType.AnsiString,mg.GroupName);
            VanfaDb.AddInParameter(cmd,"IDCard", DbType.AnsiString,mg.IDCard);
            VanfaDb.AddInParameter(cmd,"MemberCount",DbType.Int32,mg.MemberCount);
            VanfaDb.AddInParameter(cmd,"FoundTime", DbType.DateTime,mg.FoundTime);
            VanfaDb.AddInParameter(cmd,"Location",DbType.AnsiString,mg.WorkPlace1);
            VanfaDb.AddInParameter(cmd,"Mobile1",DbType.AnsiString,uc.MobilePhone);
            VanfaDb.AddInParameter(cmd,"Mobile2",DbType.AnsiString,uc.iPhone);
            VanfaDb.AddInParameter(cmd,"QQ",DbType.AnsiString,uc.QQ);
            VanfaDb.AddInParameter(cmd,"Wechat",DbType.AnsiString,uc.Wechat);
            VanfaDb.AddInParameter(cmd,"Email",DbType.AnsiString,uc.Email);
            VanfaDb.AddInParameter(cmd,"Webo",DbType.AnsiString,uc.SinaWebo);
            VanfaDb.AddInParameter(cmd,"GroupType",DbType.Int32,mg.GroupType);
            VanfaDb.AddInParameter(cmd, "Icon", DbType.AnsiString, mg.IconPath);
            return VanfaDb.ExecuteNonQuery(cmd);
        }

        public userMemberInfo_Private SelectUserProfile(int userID)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_User_SelectPrivProfile");
            VanfaDb.AddInParameter(cmd, "UserID", DbType.Int32, userID);
            using (var read = VanfaDb.ExecuteReader(cmd))
            {
                if (read.Read())
                {
                     return ConvertTo<userMemberInfo_Private>(read);
                }
                return null;
            }
        }

        public userMemberInfo_Group SelectGroupProfile(int userID)
        {
            var cmd = VanfaDb.GetStoredProcCommand("up_User_SelectGroupProfile");
            VanfaDb.AddInParameter(cmd, "UserID", DbType.Int32, userID);
            using (var read = VanfaDb.ExecuteReader(cmd))
            {
                if (!read.Read())
                    return null;
                return ConvertTo<userMemberInfo_Group>(read);
            }
        }
    }
}
